package com.hymjweb.demo.hymj.basic_data_manage.xt_bank.service;

import com.hymjweb.demo.framework.cache.service.GlobalCacheService;
import com.hymjweb.demo.framework.result.MIniUIGridResultMap;
import com.hymjweb.demo.framework.sys.BaseService;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.basic_data_manage.xt_bank.bean.XtBankPojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_bank.mapper.XtBankMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class XtBankService extends BaseService {

    @Autowired
    private XtBankMapper xtBankMapper;

    @Autowired
    private GlobalCacheService globalCacheService;


    public List<XtBankPojo> findCacheAll(){
        List<XtBankPojo> bankPojoList = xtBankMapper.findCacheAll();
        return bankPojoList;
    }


    public Object getPageList(Map map) {
        this.pageListInit(map);
        List list = xtBankMapper.findAll(map);
        MIniUIGridResultMap resultMap = this.pageToGrid(list);
        return resultMap;
    }

    @Transactional(rollbackFor = {Exception.class})
    public int insert(XtBankPojo pojo) throws Exception {
        if (pojo == null) {
            throwException("银行新增参数错误");
        }

        // 设置公司对象ID
        pojo.setId(this.getSysStrId());

        Map<String,Object> param = new HashMap<>();
        param.put("keyName",pojo.getName());
        List<XtBankPojo> xtBankPojoList = xtBankMapper.findAll(param);

        if(xtBankPojoList!=null && !xtBankPojoList.isEmpty()){
            throwException("待添加的银行名称在系统中已存在!");
        }

        int affectRows = xtBankMapper.insert(pojo);
        if (affectRows > SysConstant.INT_ZERO) {
            globalCacheService.loadXtBankCache();
        }
        return affectRows;
    }

    @Transactional(rollbackFor = {Exception.class})
    public int update(XtBankPojo pojo) throws Exception {
        if (pojo == null) {
            throwException("银行更新参数错误");
        }
        int affectRows = xtBankMapper.update(pojo);

        if (affectRows > SysConstant.INT_ZERO) {
            globalCacheService.loadXtBankCache();
        }
        return affectRows;
    }

    @Transactional(rollbackFor = {Exception.class})
    public int delete(String id) throws Exception {
        if (DataTypeUtil.isEmptyStr(id)) {
            throwException("银行删除参数错误");
        }
        int affectRows = xtBankMapper.delete(id);
        if (affectRows > SysConstant.INT_ZERO) {
            globalCacheService.loadXtBankCache();
        }
        return affectRows;
    }

}
